<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .box {
        width: 1000px;
        height: 600px;

        margin-top: 60px;
        margin-left: 30px;
      }
      .box-left {
        width: 400px;
        height: 400px;
        border: 1px solid blue;
        position: relative;
        cursor: pointer;
        float: left;
      }
      .box-left img {
        width: 100%;
        height: 100%;
      }
      .box-show {
        width: 200px;
        height: 200px;
        background-color: #000;
        opacity: 0.2;
        display: none;
        position: absolute;
        top: 0;
        left: 0;
      }
      .box-right {
        width: 500px;
        height: 400px;
        /* background-color: aqua; */
        position: relative;
        float: left;
        display: none;
        overflow: hidden;
      }
      .box-right img {
        width: 600px;
        height: 600px;
        margin-top: -30px;
        position: absolute;
        top: 0;
        left: 0;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="box-left">
        <img src="./img/apple.png" alt="" />
        <div class="box-show"></div>
      </div>
      <div class="box-right">
        <img src="./img/apple.png" alt="" class="bigImg"  />
      </div>
    </div>

    <script>
      let boxFlet = document.querySelector(".box-left");
      let boxRight = document.querySelector(".box-right");
      let boxShow = document.querySelector(".box-show");


      boxFlet.addEventListener("mouseover", function () {
        boxShow.style.display = "block";
        boxRight.style.display = "block";
      });

      boxFlet.addEventListener("mouseout", function () {
        boxShow.style.display = "none";
        boxRight.style.display = "none";
      });
      boxFlet.addEventListener("mousemove", function (e) {
        //1.选计算出鼠标在盒子内的坐标
        let x = e.pageX - this.offsetLeft;
        let y = e.pageY - this.offsetTop;

        let maskMax = boxFlet.offsetWidth - boxShow.offsetWidth;

        let boxShowX = x - boxShow.offsetWidth / 2;
        let boxShowY = y - boxShow.offsetHeight / 2;
        if (boxShowX <= 0) {
          boxShowX = 0;
        } else if (boxShowX >= boxFlet.offsetWidth - boxShow.offsetWidth) {
          boxShowX = maskMax;
        }
        if (boxShowY <= 0) {
          boxShowY = 0;
        } else if (boxShowY >= boxFlet.offsetHeight - boxShow.offsetHeight) {
          boxShowY = maskMax;
        }
        boxShow.style.left = boxShowX + "px";
        boxShow.style.top = boxShowY + "px";

         let bigImg = document.querySelector(".bigImg");

        //  let bigMax = boxRight.offsetWidth - bigImg.offsetWidth;
          let bigMax = bigImg.offsetWidth - boxRight.offsetWidth ;
        let bigX = (boxShowX * bigMax) / maskMax;
        let bigY = (boxShowY * bigMax) / maskMax;
        bigImg.style.left = -bigX + "px";
        bigImg.style.top = -bigY + "px";
      });
    </script>
  </body>
</html>
